package com.xbongbong.outerlink.config;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.OuterLinkAddRemoveAttrEnum;
import com.xbongbong.paas.enums.OuterLinkFilterRemoveAttrEnum;
import com.xbongbong.paas.enums.OuterLinkShareRemoveAttrEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.outerlink.AbstractPaasOuterLinkConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.saas.enums.OuterLinkTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.SaasGetHelp;
import com.xbongbong.saas.model.PaasFormDataModel;
import com.xbongbong.saas.model.WorkReportModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * pro 默认外链设置类
 * @author zheng.li
 **/
@Service
public class ProOuterLinkConfigServiceImpl extends AbstractPaasOuterLinkConfigService {

    @Resource
    private PaasFormDataModel paasFormDataModel;

    @Resource
    private WorkReportModel workReportModel;

    @Resource
    private SaasGetHelp saasGetHelp;


    @Override
    public PaasFormDataEntityExt getFormData(Long dataId, String corpid, Integer businessType) throws XbbException {
        PaasFormDataEntityExt paasFormDataEntityExt;
        if ( Objects.equals(businessType,0) ) {
            paasFormDataEntityExt = paasFormDataModel.getByKey(dataId,corpid);
        } else if (Objects.equals(businessType, XbbRefTypeEnum.WORK_REPORT.getCode())) {
            paasFormDataEntityExt = workReportModel.getByKey(dataId,corpid);
        } else {
            paasFormDataEntityExt = saasGetHelp.getFormData(dataId,corpid,businessType);
        }
        return paasFormDataEntityExt;
    }


    @Override
    public void removeUnsupportedAttr(Integer businessType, List<FieldAttrEntity> attrList, OuterLinkTypeEnum typeEnum) {
        if(typeEnum == null) {
            return;
        }
        List<String> removeAttrList = null;
        switch (typeEnum) {
            case SEARCH:
                removeAttrList = OuterLinkFilterRemoveAttrEnum.getAttrList(businessType);
                break;
            case SHARE:
                removeAttrList = OuterLinkShareRemoveAttrEnum.getAttrList(businessType);
                break;
            case WRITE:
                removeAttrList = OuterLinkAddRemoveAttrEnum.getAttrList(businessType);
                break;
            default:
                break;
        }
        if (CollectionsUtil.isNotEmpty(removeAttrList)) {
            List<String> finalRemoveAttrList = removeAttrList;
            attrList.removeIf(item -> finalRemoveAttrList.contains(item.getAttr()));
        }
    }

}
